package com.shanjupay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.merchant.api.AppService;
import com.shanjupay.merchant.api.dto.AppDTO;
import com.shanjupay.merchant.convert.AppConvert;
import com.shanjupay.merchant.entity.App;
import com.shanjupay.merchant.entity.Merchant;
import com.shanjupay.merchant.mapper.AppMapper;
import com.shanjupay.merchant.mapper.MerchantMapper;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

/**
 * <h1>商户创建应用服务接口实现</h1>
 * @author lsy
 * @date 2020/05/11
 */
@org.apache.dubbo.config.annotation.Service
public class AppServiceImpl implements AppService {

    @Autowired
    AppMapper appMapper;

    @Autowired
    MerchantMapper merchantMapper;

    /**
     *
     * @param merchantId 商户ID
     * @param appDTO {@link AppDTO}
     * @return
     * @throws BusinessException
     */
    @Override
    public AppDTO createApp(Long merchantId, AppDTO appDTO) throws BusinessException {
        // 数据校验
        if (merchantId == null || appDTO == null || StringUtils.isBlank(appDTO.getAppName())) {
            throw new BusinessException(CommonErrorCode.E_300009);
        }

        // 验证商户是否存在
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (merchant == null) {
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        String auditStatus = merchant.getAuditStatus();
        // 校验商户是否通过资质审核
        if (!"2".equals(auditStatus)) {
            throw new BusinessException(CommonErrorCode.E_200003);
        }
        // 应用名称需要校验唯一性
        String appName = appDTO.getAppName();
        Boolean existAppName = isExistAppName(appName);

        if (existAppName) {
            throw new BusinessException(CommonErrorCode.E_200004);
        }

        // 生成应用ID UUID
        String appId = UUID.randomUUID().toString();

        App entity = AppConvert.INSTANCE.dto2entity(appDTO);

        entity.setAppId(appId);
        entity.setMerchantId(merchantId);

        // 调用Mapper插入表中
        appMapper.insert(entity);

        // 返回DTO给Api接口
        return AppConvert.INSTANCE.entity2dto(entity);
    }

    /**
     * 根据商户ID返回app列表
     * @param merchantId 商户ID
     * @return {@link AppDTO}
     * @throws BusinessException
     */
    @Override
    public List<AppDTO> queryAppByMerchant(Long merchantId) throws BusinessException {
        // 校验数据
        if (merchantId == null) {
            throw new BusinessException(CommonErrorCode.E_300009);
        }

        // 根据商户查找相关的app应用
        List<App> apps = appMapper.selectList(new QueryWrapper<App>().lambda().eq(App::getMerchantId, merchantId));
        List<AppDTO> appDTOS = AppConvert.INSTANCE.listentity2dto(apps);
        return appDTOS;
    }

    /**
     * 查询app应用详情
     * @param appId app的ID
     * @return {@link AppDTO}
     * @throws BusinessException
     */
    @Override
    public AppDTO getAppById(String appId) throws BusinessException {
        if (appId == null) {
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        App app = appMapper.selectOne(new QueryWrapper<App>().lambda().eq(App::getAppId, appId));
        return AppConvert.INSTANCE.entity2dto(app);
    }

    /**
     * 校验AppName是否存在
     * @param appName 应用名
     * @return 数量
     */
    private Boolean isExistAppName(String appName) {
        Integer count = appMapper.selectCount(new LambdaQueryWrapper<App>().eq(App::getAppName, appName));
        return count > 0;
    }
}
